package com.xykj.enterprise.wechat.ext.api.externalcontact;

import com.xykj.enterprise.wechat.bean.ext.externalcontact.TransferGroupChatVo;
import com.xykj.enterprise.wechat.ext.service.customer.ExternalContactService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "分配离职成员的客户群", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "chat_id_list", type = Type.String, description = "需要转群主的客户群ID列表。取值范围： 1 ~ 100", required = true),
        @Parameter(name = "new_owner", type = Type.String, description = "新群主ID", required = true),
})
@Component
public class TransferGroupChat implements Action {

    @Autowired
    ExternalContactService externalContactService;

    @Override
    public Response execute(Request request) throws Exception {
        String chat_id_list = request.getString("chat_id_list");
        String new_owner = request.getString("new_owner");
        HashMap<String, Object> params = new HashMap<String, Object>();
        String[] chatIdListSplit = chat_id_list.split(",");
        params.put("chat_id_list", chatIdListSplit);
        params.put("new_owner", new_owner);

        TransferGroupChatVo rsp = externalContactService.transferGroupChat(
                request.getString("access_token"), params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }
}
